import { ExampleService } from "./index";

export enum servicesEnum {
  exampleService = 'exampleService'
} 

export class ServiceFactory{
  public static current = new ServiceFactory()

  private map = new Map()
  private serviceGenerator = [
    { name: servicesEnum.exampleService, loader: () => new ExampleService() }
  ]

  public getService<T>(name: servicesEnum):T{
    const service = this.map.get(name)
    if (service) {
      return service
    }
    this.map.set(name, this.generateService(name))
    return this.getService(name)
  }

  private generateService(name: servicesEnum) {
    const generator = this.serviceGenerator.find(service => service.name === name)
    if (!generator) {
      throw new Error(`can't find service by ${name}`)
    }
    return generator.loader()
  }
}